/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.EnvironmentMonitorLog;
import me.zhengjie.modules.biz.domain.Lab;
import me.zhengjie.modules.biz.repository.LabRepository;
import me.zhengjie.modules.biz.service.dto.EnvironmentMonitorDto;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.biz.repository.EnvironmentMonitorLogRepository;
import me.zhengjie.modules.biz.service.EnvironmentMonitorLogService;
import me.zhengjie.modules.biz.service.dto.EnvironmentMonitorLogDto;
import me.zhengjie.modules.biz.service.dto.EnvironmentMonitorLogQueryCriteria;
import me.zhengjie.modules.biz.service.mapstruct.EnvironmentMonitorLogMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author Admin
* @date 2023-02-20
**/
@Service
@RequiredArgsConstructor
public class EnvironmentMonitorLogServiceImpl implements EnvironmentMonitorLogService {

    private final EnvironmentMonitorLogRepository environmentMonitorLogRepository;
    private final EnvironmentMonitorLogMapper environmentMonitorLogMapper;
    private final LabRepository labRepository;

    @Override
    public Map<String,Object> queryAll(EnvironmentMonitorLogQueryCriteria criteria, Pageable pageable){
        Page<EnvironmentMonitorLog> page = environmentMonitorLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<EnvironmentMonitorLogDto> dto = page.map(environmentMonitorLogMapper::toDto);
        List<EnvironmentMonitorLogDto> resultDto = dto.getContent();
        for (EnvironmentMonitorLogDto environmentMonitorLogDto:resultDto) {
            Lab lab = labRepository.findById(environmentMonitorLogDto.getLabId()).orElseGet(Lab::new);
            environmentMonitorLogDto.setLabName(lab.getName());
            String adminName = labRepository.findByadminId(environmentMonitorLogDto.getAdministrator()).get("adminName");
            environmentMonitorLogDto.setAdminName(adminName);
        }
        return PageUtil.toPage(dto);
    }

    @Override
    public List<EnvironmentMonitorLogDto> queryAll(EnvironmentMonitorLogQueryCriteria criteria){
        return environmentMonitorLogMapper.toDto(environmentMonitorLogRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public EnvironmentMonitorLogDto findById(Integer id) {
        EnvironmentMonitorLog environmentMonitorLog = environmentMonitorLogRepository.findById(id).orElseGet(EnvironmentMonitorLog::new);
        ValidationUtil.isNull(environmentMonitorLog.getId(),"EnvironmentMonitorLog","id",id);
        return environmentMonitorLogMapper.toDto(environmentMonitorLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public EnvironmentMonitorLogDto create(EnvironmentMonitorLog resources) {
        return environmentMonitorLogMapper.toDto(environmentMonitorLogRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(EnvironmentMonitorLog resources) {
        EnvironmentMonitorLog environmentMonitorLog = environmentMonitorLogRepository.findById(resources.getId()).orElseGet(EnvironmentMonitorLog::new);
        ValidationUtil.isNull( environmentMonitorLog.getId(),"EnvironmentMonitorLog","id",resources.getId());
        environmentMonitorLog.copy(resources);
        environmentMonitorLogRepository.save(environmentMonitorLog);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            environmentMonitorLogRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<EnvironmentMonitorLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (EnvironmentMonitorLogDto environmentMonitorLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("环境监控仪", environmentMonitorLog.getEnvironmentMonitorId());
            map.put("名称", environmentMonitorLog.getName());
            map.put("实验室", environmentMonitorLog.getLabId());
            map.put("负责人", environmentMonitorLog.getAdministrator());
            map.put("温度", environmentMonitorLog.getTemperature());
            map.put("温度阈值", environmentMonitorLog.getTemperatureThresholdValue());
            map.put("湿度", environmentMonitorLog.getHumidity());
            map.put("湿度阈值", environmentMonitorLog.getHumidityThresholdValue());
            map.put("压差", environmentMonitorLog.getPressure());
            map.put("压差阈值", environmentMonitorLog.getPressureThresholdValue());
            map.put("报警信息", environmentMonitorLog.getErrorMessage());
            map.put(" createdAt",  environmentMonitorLog.getCreatedAt());
            map.put(" updatedAt",  environmentMonitorLog.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
